﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI.XWPF.UserModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Excel2doc
{
    public class BookMarks
    {
        /// <summary>
        /// 书签Name
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// 书签文本
        /// </summary>
        public string Text
        {
            get;
            set;
        }

    }
    class NDOC
    {        
        public static void ToDocx(IEnumerable<BookMarks> lBookMarks, string sTemplatePath, string sFileName)
        {
            FileStream fs = new FileStream(sTemplatePath, FileMode.Open, FileAccess.Read);
            var doc = new XWPFDocument(fs);
            replaceInPara(doc, lBookMarks);
            replaceInTable(doc, lBookMarks);
            FileStream fos = new FileStream(sFileName, FileMode.Create);
            doc.Write(fos);
            fs.Close(); 
            fos.Close();
        }
        //替换段落里面的变量 
        private static void replaceInPara(IEnumerable<XWPFParagraph> paragraphs, IEnumerable<BookMarks> lBookMarks)
        {
            //var paragraphs = doc.Paragraphs; //List<XWPFParagraph>
            foreach (var item in paragraphs)
            {
                foreach (var i in item.Runs)
                {
                    //MessageBox.Show(i.GetText(0));
                    foreach (BookMarks m in lBookMarks)
                    {
                        //Debug.Write("【excel，run内容】："+i.GetText(0)+"\n");
                        //Debug.Write("【map书签】：" + m.Name + "\n");
                        //Debug.Write("【map内容】：" + m.Text + "\n");
                        if (i.GetText(0) == m.Name)
                        {
                            i.SetText(m.Text, 0);
                        }
                    }
                }
            }
        }
        //替换段落里面的变量 
        private static void replaceInPara(XWPFDocument doc, IEnumerable<BookMarks> lBookMarks)
        {
            var paragraphs = doc.Paragraphs; //List<XWPFParagraph>
            if (paragraphs.Count > 0)
                replaceInPara(paragraphs, lBookMarks);
        }


        //替换表格里面的变量
        private static void replaceInTable(XWPFDocument doc, IEnumerable<BookMarks> lBookMarks)
        {
            var tables = doc.Tables;
            if (tables.Count > 0)
            {
                foreach (var t in tables)
                {
                    foreach (var r in t.Rows)
                    {
                        foreach (var c in r.GetTableCells())
                        {
                            //Debug.Write("【table】");
                            replaceInPara(c.Paragraphs, lBookMarks);
                        }
                    }
                }
            }
        }
        //var doc = new XWPFDocument();
        //var p1 = doc.CreateParagraph();
        ////p1.SetAlignment(ParagraphAlignment.LEFT);
        //var r1 = p1.CreateRun();
        //r1.SetBold(true);
        //r1.SetText("The quick brown fox");
        //r1.SetText("Hello2");

        //p1 = doc.CreateParagraph();
        ////p1.SetAlignment(ParagraphAlignment.LEFT);
        //r1 = p1.CreateRun();
        //r1.SetBold(true);
        //r1.SetText("The quick brown fox");
        //r1.SetText("Hello2");

        //FileStream sw = File.OpenWrite("blank.docx");
        //doc.Write(sw);
        //sw.Close();
        //Process.Start("blank.docx");
    }
}
